# coding:utf-8

from common.entities import Category, Categories
from common.htmlparser import HtmlParser
from common.utils import cstrtime
import json


class ProductDetailsParser:
    __data = None

    def load_json(self, json_data):
        self.__data = json.loads(json_data)

    def __del__(self):
        self.__data = None

    def find_id(self):
        try:
            style_id = self.__data['style_id']
            return style_id
        except KeyError as e:
            print('[Error Json KeyError] ' + str(e))

    def find_name(self):
        try:
            name = self.__data['main_text']
            return name
        except KeyError as e:
            print('[Error Json KeyError] ' + str(e))

    def find_price(self, style_id):
        try:
            price = self.__data['style_wrappers'][style_id]['price']
            if price is None or len(price) < 2:
                return None
            return price[1:len(price)]
        except KeyError as e:
            print('[Error Json KeyError] ' + str(e))
            return None

    def find_currency(self, style_id):
        try:
            price = self.__data['style_wrappers'][style_id]['price']
            if price is None or len(price) < 1:
                return None
            return price[0:1]
        except KeyError as e:
            print('[Error Json KeyError] ' + str(e))
            return None

    def findall_desc(self):
        try:
            desc = self.__data['bullets']
            return desc
        except KeyError as e:
            print('[Error Json KeyError] ' + str(e))
            return None

    def findall_zoomed_images(self):
        try:
            images = self.__data['images']['web_zoomin']
            if len(images) == 0:
                return None
            return images
        except KeyError as e:
            print('[Error Json KeyError] ' + str(e))
            return None

    def findall_main_images(self):
        try:
            images = self.__data['images']['web_full']
            if len(images) == 0:
                return None
            return images
        except KeyError as e:
            print('[Error Json KeyError] ' + str(e))
            return None


class ProductUrlParser:
    __parser = None

    def __init__(self):
        self.__parser = HtmlParser()
        self.__parser.addrule('ids', r'\'(.*?)\'')
        self.__parser.addrule('styles', r'GUCCI.Category.styles = \[(.*?)\]')

    def __del__(self):
        self.__parser = None

    def findall_ids(self, src):
        ids = self.__parser.findall('ids', src)
        if len(ids) > 0:
            return ids
        else:
            return None

    def findall_styles(self, src):
        styles = self.__parser.findall('styles', src)
        if len(styles) > 0:
            return styles
        else:
            return None

    @staticmethod
    def compose_url(site_url, product_id):
        if product_id is None or len(product_id) == 0:
            return None
        return site_url + '/styles/' + product_id


class CategoryUrlParser:
    _parser = HtmlParser()

    def __init__(self):
        self._parser.addrule('mega', r'mega_menu">(.*?)<li class="mega_menu worldofg"')
        self._parser.addrule('lines', r'<li>(.*?)</li>')
        self._parser.addrule('titles', r'<a href=".*?" hreflang=".*?">(.*?)</a>')
        self._parser.addrule('urls', r'<a href="(.*?)" hreflang=".*?">.*?</a>')

    def __del__(self):
        self._parser = None

    def __findall_titles(self, src):
        titles = self._parser.findall('titles', src)
        if len(titles) > 0:
            return titles
        else:
            return None

    def __findall_urls(self, src):
        urls = self._parser.findall('urls', src)
        if len(urls) > 0:
            return urls
        else:
            return None

    def findall(self, src):
        mega = self._parser.findall('mega', src)
        if len(mega) == 0:
            return None
        lines = self._parser.findall('lines', mega[0])
        if len(lines) == 0:
            return None
        cats = Categories()
        for line in lines:
            titles = self._parser.findall('titles', line)
            urls = self._parser.findall('urls', line)
            if len(titles) > 0:
                title = titles[0]
            else:
                title = None
            if len(urls) > 0:
                url = urls[0]
            else:
                print('_err: ' + line)
                continue
            cat = Category(url, title, url, cstrtime())
            cat.has_child = False
            cats.put_cat(cat)
        return cats
